﻿using MDIDemo.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_EquipmentBase : Form_BaseDialog
    {
        public Form_EquipmentBase()
        {
            InitializeComponent();
        }
        public EquipmentModel equipmentModel;
        public void Ini()
        {
            base.SetComponent();
        }
        /// <summary>
        /// 窗体状态
        /// </summary>
        /// <param name="WinType">-1：查看，0：修改，1：增加</param>
        public void SetWinType(int WinType)
        {
            base.WinType = WinType;
        }
        private void spinEdit1_EditValueChanged(object sender, EventArgs e)
        {
            this.textEdit4.Text = this.spinEdit1.Value.ToString();
        }

        protected override void DisplayComponent(string id)
        {
            base.class_ComboBoxEdit.SetComboBoxEdit(this.comboBoxEdit1
                , base.class_SQLiteOperator.GetAllDictionary("AAA").ToArray());
//            base.class_SetTextEdit.SetTextEdit(this.textEdit1, false, Color.Black);
  //          base.class_SetTextEdit.SetTextEdit(this.textEdit2, false, Color.Black);
    //        base.class_SetTextEdit.SetTextEdit(this.textEdit3, false, Color.Black);
      //      base.class_SetTextEdit.SetTextEdit(this.textEdit4, false, Color.Black);
            base.class_SetMemoEdit.SetMemoEdit(this.memoEdit1, false);

            if (id != null)
            {
                this.spinEdit1.ReadOnly = true;
                string Sql = string.Format("Select * From vou_equipment Where id ='{0}'", id);
                DataTable dataTable = base.class_MySqlDataBase.GetDataTable(Sql);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    DataRow dataRow = dataTable.Rows[0];
                    if (dataRow != null)
                    {
                        this.spinEdit1.Value = int.Parse(dataRow["id"].ToString());
                        this.textEdit1.Text = dataRow["equipmentName"].ToString();
                        this.textEdit2.Text = dataRow["factoryName"].ToString();
                        this.textEdit3.Text = dataRow["ip"].ToString();
                        //this.textEdit4.Text = dataRow["id"].ToString();
                        this.memoEdit1.Text = dataRow["remark"].ToString();
                        this.comboBoxEdit1.Text = dataRow["equipmentType"].ToString();
                        this.spinEdit2.Value = int.Parse(dataRow["port"].ToString());
                        this.spinEdit3.Value = int.Parse(dataRow["sensorCount"].ToString());
                    }
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (this.textEdit1.Text == "" || this.textEdit1.Text.Length == 0)
            {
                MessageBox.Show("设备名称不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (WinType == 1 && base.class_MySqlDataBase.GetKeyCount("vou_equipment", "id", "id", this.spinEdit1.Value.ToString()) > 0)
            {
                MessageBox.Show("设备唯一标识不允许重复！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (this.textEdit3.Text.Length > 0)
            {
                string ipStr = this.textEdit3.Text;
                IPAddress ip;
                if (!IPAddress.TryParse(ipStr, out ip))
                {
                    MessageBox.Show("IP地址不正确！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            equipmentModel = new EquipmentModel();
            equipmentModel.id = this.spinEdit1.Value.ToString();
            equipmentModel.equipmentName = this.textEdit1.Text;
            equipmentModel.factoryName = this.textEdit2.Text;
            equipmentModel.ip = this.textEdit3.Text;
            equipmentModel.remark = this.memoEdit1.Text;
            equipmentModel.equipmentType = this.comboBoxEdit1.Text;
            equipmentModel.port = this.spinEdit2.Value.ToString();
            equipmentModel.sensorCount = int.Parse(this.spinEdit3.Value.ToString());
            this.DialogResult = DialogResult.OK;
        }
    }
}
